﻿using System;
using Halfpipe.Data.Repository;
using Halfpipe.Data.Service;
using System.Data.Objects;

namespace Halfpipe.Data.Service
{
    public abstract class UnitOfWork<TContext> : IUnitOfWork where TContext: ObjectContext, new()
    {
        private TContext Context { get; set; }

        protected UnitOfWork()
        {
            Context = new TContext();
        }

        public int Commit()
        {
            return Context.SaveChanges();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (Context != null)
                {
                    Context.Dispose();
                    Context = null;
                }
            }
        }

        public IRepository<TEntity> EntityRepositoryFor<TEntity>() where TEntity : class
        {
            return new EntityRepository<TEntity>(Context);
        }
    }
}
